<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">网站名称</label>
        <el-select
          v-model="query.webId"
          class="filter-item"
          filterable
          clearable
          placeholder="网站名称"
          style="width: 185px;"
        >
          <el-option
            v-for="item in dict.label.spider_web"
            :key="item.webId"
            :label="item.webName"
            :value="item.webId"
          />
        </el-select>
        <label class="el-form-item-label">漫画名字</label>
        <el-input
          v-model="query.alt"
          clearable
          placeholder="漫画名字"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <label class="el-form-item-label">作者</label>
        <el-input
          v-model="query.author"
          clearable
          placeholder="作者"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <!--        <label class="el-form-item-label">漫画分区</label>-->
        <!--        <el-input v-model="query.type" clearable placeholder="漫画分区" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />-->
        <label class="el-form-item-label">漫画标签</label>
        <el-input
          v-model="query.tag"
          clearable
          placeholder="漫画标签"
          style="width: 185px;"
          class="filter-item"
          @keyup.enter.native="crud.toQuery"
        />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="网站名称">
            <el-select v-model="form.webId" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.label.spider_web"
                :key="item.webId"
                :label="item.webName"
                :value="item.webId"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="漫画名字" prop="alt">
            <el-input v-model="form.alt" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="作者">
            <el-input v-model="form.author" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="封面图" prop="img">
            <el-input v-model="form.img" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="漫画详情页地址">
            <el-input v-model="form.href" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="漫画分区">
            <el-input v-model="form.type" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="漫画标签">
            <el-input v-model="form.tag" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="简介">
            <el-input v-model="form.remark" :rows="3" type="textarea" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        v-loading="crud.loading"
        :data="crud.data"
        size="small"
        style="width: 100%;"
        @row-dblclick="jumpToRead"
        @selection-change="crud.selectionChangeHandler"
      >
        <el-table-column width="55" prop="comicId" label="ID" />
        <el-table-column prop="webId" label="网站名称" width="100">
          <template slot-scope="scope">
            {{ dict.label.spider_web[scope.row.webId].webName }}
          </template>
        </el-table-column>
        <el-table-column prop="alt" label="漫画名字" />
        <el-table-column prop="img" label="封面图" width="120">
          <template slot-scope="scope">
            <el-image
              :src="scope.row.localPath ? getLocalImgFile(scope.row.localPath) : ''"
              :preview-src-list="scope.row.localPath ? [getLocalImgFile(scope.row.localPath)] : []"
            >
              <div slot="error" class="image-slot">
                <el-image
                  :src="scope.row.img"
                  fit="cover"
                  :preview-src-list="[scope.row.img]"
                />
              </div>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column prop="href" label="漫画详情页地址" />
        <el-table-column prop="author" label="作者" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag
              v-for="(tag, index) in (scope.row.author ? scope.row.author.split(',') : [''])"
              :key="index"
              :type="randomColor(index)"
              class="mx-1"
              effect="plain"
            >{{ tag }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="tag" label="标签" width="300" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-tag
              v-for="(tag, index) in (scope.row.tag ? scope.row.tag.split(',') : [''])"
              :key="index"
              :type="randomColor(index)"
              class="mx-1"
              effect="plain"
            >{{ tag }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="简介" />
        <el-table-column prop="updateTime" label="更新时间" />
        <el-table-column
          v-if="checkPer(['admin','spiderComic:edit','spiderComic:del'])"
          label="操作"
          width="200px"
          align="center"
        >
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            >
              <el-dropdown trigger="click">
                <el-button type="success">
                  更多<i class="el-icon-arrow-down el-icon--right" />
                </el-button>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item>
                    <el-button
                      v-permission="['admin','spiderComic:list']"
                      size="mini"
                      style="margin-right: 3px;"
                      icon="el-icon-reading"
                      type="text"
                      @click="jumpToRead(scope.row)"
                    >阅读
                    </el-button>
                  </el-dropdown-item>
                  <el-dropdown-item>
                    <el-button
                      v-permission="['admin','spiderComic:list']"
                      size="mini"
                      style="margin-right: 3px;"
                      icon="el-icon-video-play"
                      type="text"
                      @click="refreshSpiderComicDetail(scope.row)"
                    >更新
                    </el-button>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </udOperation>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudSpiderComic from '@/api/spider/spiderComic'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { randomColor, getLocalImgFile } from '@/utils/spiderUtil'

const defaultForm = {
  comicId: null,
  webId: null,
  alt: null,
  author: null,
  img: null,
  href: null,
  type: null,
  tag: null,
  deletedFlag: null,
  remark: null,
  updateTime: null,
  createTime: null
}
export default {
  name: 'SpiderComic',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['user_status', 'spider_web', 'server_url'],
  cruds() {
    return CRUD({
      title: '漫画管理',
      url: 'api/spiderComic',
      idField: 'comicId',
      sort: 'comicId,desc',
      crudMethod: { ...crudSpiderComic }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'spiderComic:add'],
        edit: ['admin', 'spiderComic:edit'],
        del: ['admin', 'spiderComic:del']
      },
      rules: {
        webId: [
          { required: true, message: '所属网站不能为空', trigger: 'blur' }
        ],
        alt: [
          { required: true, message: '漫画名字不能为空', trigger: 'blur' }
        ],
        img: [
          { required: true, message: '封面图不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'webId', display_name: '所属网站' },
        { key: 'alt', display_name: '漫画名字' },
        { key: 'author', display_name: '作者' },
        { key: 'type', display_name: '漫画分区' },
        { key: 'tag', display_name: '漫画标签' }
      ]
    }
  },
  methods: {
    randomColor, getLocalImgFile,
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    jumpToRead(data) {
      this.$router.push({
        name: 'SpiderComicDetail',
        params: data
      })
    },
    refreshSpiderComicDetail(data) {
      crudSpiderComic.refreshSpiderComicDetail(data)
    }
  }
}
</script>

<style scoped>

</style>
